layui.use(['table', 'form', 'jquery', 'treetable', 'backend'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;
    var backend = layui.backend;
    var treetable = layui.treetable;

    var render = function () {
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            skin: 'line',
            method: 'post',
            treeDefaultClose: true,
            toolbar: '#toolbar',
            elem: '#table',
            url: '/admin/role/index',
            page: false,
            cols: [
                [
                    {type: 'checkbox'},
                    {field: 'id', title: '角色id'},
                    {field: 'nickname', title: '名称'},
                    {field: 'status', title: '状态', templet: '#status'},
                    {title: '操作', templet: '#bar', align: 'center'}
                ]
            ]
        })
    };

    render();

    table.on('tool(table)', function (obj) {
        try {
            if(typeof(eval("event_" + obj.event)) == "function") {
                eval("event_" + obj.event + "(obj);")
            }
        } catch(e) {
            layui.hint().error("未找到事件方法" + "event_" + obj.event + ":" + e);
        }
    });

    table.on('toolbar(table)', function (obj) {
        try {
            if(typeof(eval("event_" + obj.event)) == "function") {
                eval("event_" + obj.event + "(obj);")
            }
        } catch(e) {
            switch (obj.event) {
                case "expandAll":
                    treetable.expandAll("#table");
                    break;
                case "foldAll":
                    treetable.foldAll("#table");
                    break;
            }
            layui.hint().error("未找到事件方法" + "event_" + obj.event + ":" + e);
        }
    });

    function event_add(obj = null) {
        console.log(obj.data);
        var url = '/admin/role/show' + (obj == null || obj.data == undefined ? '' : '?pid=' + obj.data.id);
        backend.api.open(url, '新增', {
            callback: function () {
                event_refresh();
            }
        });
    }

    function event_edit(obj) {
        backend.api.open('/admin/role/show?id=' + obj.data.id, '修改', {
            callback: function () {
                refresh();
            }
        });
    }

    function event_remove(obj) {
        layer.confirm('如果含有子项，将一同删除，确定要删除吗？', {icon: 3, title: '提示'}, function (index) {
            layer.close(index);
            var loading = layer.load();
            $.ajax({
                url: "/admin/role/del",
                dataType: 'json',
                data: {ids: [obj.data['id']]},
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 1) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            render();
                        });
                    } else {
                        layer.confirm(res.msg, {icon: 3, title:'警告'}, function(index){
                            layer.close(index);
                        });
                    }
                },
                complete: function () {
                    layer.close(loading);
                }
            })
        });
    };

    function event_batchRemove() {
        var data = table.checkStatus('table').data;
        var ids = [];
        $.each(data, function (k, v) {
            ids.push(v.id);
        });
        var loading = layer.load();
        $.ajax({
            url: "/admin/role/del",
            dataType: 'json',
            type: 'post',
            data: {ids: ids},
            success: function (result) {
                layer.close(loading);
                if (result.code == 1) {
                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                        render();
                    });
                } else {
                    layer.confirm(res.msg, {icon: 3, title:'警告'}, function(index){
                        layer.close(index);
                    });
                }
            },
            complete: function () {
                layer.close(loading);
            }
        })
    }

    function event_refresh() {
        render();
    }
});
